package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.CollectInfo;
import com.yy.jhl.bean.ProjectDetailInfo;
import com.yy.jhl.bean.ProjectInfo;
import com.yy.jhl.interfaces.CommonCallBack;
import com.yy.jhl.ui.manager.contract.ProjectDetailContract;
import com.yy.jhl.ui.manager.presenter.ProjectDetailPresenter;
import com.yy.jhl.util.DataRequestUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * Created by libingn on 2017/12/11.
 */

public class ProjectDetailActivity extends BaseActivity<ProjectDetailPresenter> implements ProjectDetailContract.View {
    ProjectInfo.DataBean projectInfo;
    @BindView(R.id.order_name_text)
    TextView orderNameText;
    @BindView(R.id.zb_code_text)
    TextView zbCodeText;
    @BindView(R.id.zb_address_text)
    TextView zbAddressText;
    @BindView(R.id.zb_content_text)
    TextView zbContentText;
    @BindView(R.id.project_name_text)
    TextView projectNameText;
    @BindView(R.id.zhongb_name_text)
    TextView zhongbNameText;
    @BindView(R.id.zhaob_name_text)
    TextView zhaobNameText;
    @BindView(R.id.contact_name_text)
    TextView contactNameText;
    @BindView(R.id.contact_tel_text)
    TextView contactTelText;
    @BindView(R.id.agency_name_text)
    TextView agencyNameText;
    @BindView(R.id.case_text)
    TextView caseText;
    @BindView(R.id.attach_text)
    TextView attachText;
    boolean isCollected = false;
    int loginType;
    CollectInfo collectInfo;
    public static void startAction(Context mContext, ProjectInfo.DataBean projectInfo) {
        Intent intent = new Intent(mContext, ProjectDetailActivity.class);
        intent.putExtra("projectInfo", projectInfo);
        mContext.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_project_detail;
    }

    @Override
    public void initView() {
        projectInfo = (ProjectInfo.DataBean) getIntent().getSerializableExtra("projectInfo");
        showTitle("项目详情", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        loginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        //供应商显示收藏按钮
        if (loginType == 1){
            int rightImageId;
            if ("1".equals(projectInfo.getRelated())){
                isCollected = true;
                rightImageId = R.mipmap.white_full_star;
            }else {
                isCollected = false;
                rightImageId = R.mipmap.white_star;
            }
            showRightImageView(rightImageId, new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    collectInfo = new CollectInfo();
                    collectInfo.setProjectId(projectInfo.getProject_id());
                    DataRequestUtil.getInstance(mContext).collectProject(projectInfo.getProject_id(), isCollected, new CommonCallBack() {
                        @Override
                        public void getCallBack() {
                            if (isCollected){
                                collectInfo.setRelateId("0");
                                isCollected = false;
                                showRightImageView(R.mipmap.white_star);
                            }else {
                                collectInfo.setRelateId("1");
                                isCollected = true;
                                showRightImageView(R.mipmap.white_full_star);
                            }
                        }
                    });
                }
            });
        }
        mPresenter.getProjectDetail(projectInfo.getProject_id());
    }

    @Override
    public void getProjectDetail(ProjectDetailInfo projectDetailInfo) {
        orderNameText.setText(projectDetailInfo.getProject_name());
        zbCodeText.setText(projectDetailInfo.getProject_num());
        zbAddressText.setText(projectDetailInfo.getAddress());
        zbContentText.setText(projectDetailInfo.getDesc());
//        projectNameText.setText("");
        caseText.setText(projectDetailInfo.getShow_type_text());
        attachText.setText(projectDetailInfo.getAttach());

    }

    @Override
    public void getProjectDetailFail() {

    }

    @Override
    public void finish() {
        if (collectInfo != null){
            EventBus.getDefault().post(collectInfo);
        }
        super.finish();
    }
}
